package com.Maykk.Demo02;

import java.util.*;

/**
 * @ClassName: Demo01
 * @Description: TODO 利用 HashMap 统计字符串中字符出现的次数
 * @Author: MaykkStar@163.com
 */
public class Demo01 {
    public static void main(String[] args) {
        HashMap<Character, Integer> hm = new HashMap<>();
        System.out.print("请输入一个字符串：");
        String str = new Scanner(System.in).nextLine();
        for (int i = 0; i < str.length(); i++) {
            char key = str.charAt(i);
            Integer count = hm.get(key);
            if (null == count) {
                hm.put(key, 1);
            } else {
                hm.put(key, ++count);
            }
        }
        Set<Character> keySet = hm.keySet();
        for (Character key : keySet) {
            System.out.println(key + " 出现了 " + hm.get(key) + "次");
        }
    }
}
